
package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyArea;
import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResPhaseRepository;
import com.china08.yxyapi.repository.db.yxy.YxyAreaRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 数据统计 数据总览 资源统计列表    级联
 * 
 */
@CrossOrigin
@RestController
public class ResourceJiLianTwoController extends YxyApiBaseController{
	
	@Autowired
	private YxyAreaRepository yxyAreaRepository;
	@Autowired
	private YxyResPhaseRepository yxyResPhaseRepository;
	@ApiOperation(value = "资源数据总览 资源统计列表    级联")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/eachschool/jiliantwo/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel reqModel){
        RespModel model = new RespModel();
        	List<YxyArea> yxyAreaLst=new ArrayList<YxyArea>();
        	YxyArea yxyArea=null;
        	//区
        	List<String> areaNameLst=new ArrayList<String>();
        	
        	//市
        	List<String> cityNameLst=new ArrayList<String>();
        	
        	//学段
        	List<String> phaseNameLst=new ArrayList<String>();
			Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
	        String districtCode=null;
	        if (schoolBureauLevel==0) {//区
	        	districtCode=getProfile(request, response).getSchoolAreaIdDistrict();
	        	yxyArea = yxyAreaRepository.findByAreaId(districtCode);
	        	areaNameLst.add(yxyArea.getAreaName());
	        	districtCode = getProfile(request, response).getSchoolAreaIdCity();
	        	yxyArea = yxyAreaRepository.findByAreaId(districtCode);
	        	cityNameLst.add(yxyArea.getAreaName());
			}else if(schoolBureauLevel==1){//市
				districtCode=getProfile(request, response).getSchoolAreaIdCity();
				if (reqModel.cityName !=null && !reqModel.cityName.equals("")) {
					yxyArea=yxyAreaRepository.findByAreaName(reqModel.cityName);
					yxyAreaLst=yxyAreaRepository.findByAreaPId(yxyArea.getAreaId());
					for (YxyArea yxyArea1 : yxyAreaLst) {
						areaNameLst.add(yxyArea1.getAreaName());
					}
				}else{
					yxyAreaLst=yxyAreaRepository.findByAreaPId(districtCode);
					for (YxyArea yxyArea1 : yxyAreaLst) {
						//将市下面的区封装
						areaNameLst.add(yxyArea1.getAreaName());
					}
				}
				yxyArea = yxyAreaRepository.findByAreaId(districtCode);
				
				//将当前登录用户的市封装
				cityNameLst.add(yxyArea.getAreaName());
			}else if(schoolBureauLevel==2){//省				
				districtCode=getProfile(request, response).getSchoolAreaIdProvince();
				if (reqModel.cityName==null || reqModel.cityName.equals("")) {
					yxyAreaLst=yxyAreaRepository.findByAreaPId(districtCode);
					
					//获取到省下面所有的市id进行封装，为了通过市id查询得到对应的区
					List<String> cityIdLst=new ArrayList<String>();
					for (YxyArea yxyArea1 : yxyAreaLst) {
						//将省下面的市封装
						cityNameLst.add(yxyArea1.getAreaName());
						//将省下面的市id进行封装
						cityIdLst.add(yxyArea1.getAreaId());
					}
					for (String cityid : cityIdLst) {
						yxyAreaLst=yxyAreaRepository.findByAreaPId(cityid);
						for (YxyArea yxyArea1 : yxyAreaLst) {
							areaNameLst.add(yxyArea1.getAreaName());
						}
					}
				}else{
					yxyArea=yxyAreaRepository.findByAreaName(reqModel.cityName);
					yxyAreaLst=yxyAreaRepository.findByAreaPId(yxyArea.getAreaId());
					for (YxyArea yxyArea1 : yxyAreaLst) {
						areaNameLst.add(yxyArea1.getAreaName());
					}
				}
			}
	        //查所有学段名称并且进行封装
	        List<YxyResPhase> yxyResPhaseList=new ArrayList<YxyResPhase>();
	        yxyResPhaseList=yxyResPhaseRepository.findByStatus(1);
	        for (YxyResPhase yxyResPhase : yxyResPhaseList) {
	        	phaseNameLst.add(yxyResPhase.getName());
			}
	        model.areaNameLst=areaNameLst;
	        model.cityNameLst=cityNameLst;
	        model.phaseNameLst=phaseNameLst;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
    	//市
    	public List<String> cityNameLst;
    	//区
    	public List<String> areaNameLst;
    	//学段
    	public List<String> phaseNameLst;
    }
    
    protected static class ReqModel extends YxyApiPageReqModel {
    	//市名称
    	public String cityName;
    }
}
